package org.oexam.ostudy.sl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.naming.NamingException;

import org.oexam.ostudy.db.TestAccess;
import org.oexam.ostudy.db.TestSetsAccess;
import org.oexam.ostudy.db.UsersAccess;
import org.oexam.ostudy.facade.struct.InsertNewQuestionDetails;
import org.oexam.ostudy.facade.struct.TestDisplayDetails;
import org.oexam.ostudy.facade.struct.TestNameAndId;
import org.oexam.ostudy.facade.struct.ViewAndAddQuestionsPageDisplayDetails;
import org.oexam.ostudy.struct.AllResults;
import org.oexam.ostudy.struct.Questions;
import org.oexam.ostudy.struct.StudentAnswers;
import org.oexam.ostudy.struct.Students;
import org.oexam.ostudy.struct.TestAtt;
import org.oexam.ostudy.struct.Tests;

public class ManageTests {

	public String addQuestionToTest(InsertNewQuestionDetails newQuestion) throws SQLException, NamingException{
		
		TestAccess testAccess = new TestAccess();
		
        Questions ques = new Questions();
        
        ques.setQid(testAccess.getLastQuestionsID()+1);
        ques.setAnswer(newQuestion.getAnswer());
        ques.setQuestion(newQuestion.getQuestion());
        ques.setTest_id(newQuestion.getTest_id());
        ques.setOption1(newQuestion.getOption1());
        ques.setOption2(newQuestion.getOption2());
        ques.setOption3(newQuestion.getOption3());
        ques.setOption4(newQuestion.getOption4());
		
        testAccess.insertQuestion(ques);
		
		return "ok";
	}
	
	public List<TestNameAndId> getAllTestsLinkedToTeacher(int userID) throws SQLException, NamingException{
		
		int teacherID = new UsersAccess().getTeacherIDUsingUserID(userID);
		return new TestAccess().getAllTestsLinkedToTeacher(teacherID);
	}
	
	public ViewAndAddQuestionsPageDisplayDetails getVAQuestionPageDetails(int testID) throws SQLException, NamingException{
		TestAccess testAccess = new TestAccess();
		
		ViewAndAddQuestionsPageDisplayDetails details = new ViewAndAddQuestionsPageDisplayDetails();
		details.setQuestionsList(testAccess.getAllQuestionsForATest(testID));
	
		return details;
	}

	public List<TestNameAndId> getAllTestsThatStudentIsEligibleFor(int studentID) throws SQLException, NamingException {
		
		UsersAccess usersAccess = new UsersAccess();
	    TestAccess testAccess = new TestAccess();
	    
	    
		Students student = usersAccess.getStudentUsingJDBC(studentID);
		
		// TODO Prasanna : Very IMPORTANT: Logic has to change here. Tests listed for a student must not be dependent on teacher.
		
		
		List<TestNameAndId> testList=testAccess.getAllTestsLinkedToTeacher(student.getTeacher_responsible());
		for(TestNameAndId tn : testList){
			if(testAccess.hasStudentAttandedthisTest(studentID, tn.getId())){
				testList.remove(tn);
			}
		}
		
		return testList;
	}

	public void startTest(int testID, int studentID) throws Exception {
		
		TestAccess testAccess = new TestAccess();
		if(!testAccess.hasStudentAttandedthisTest(studentID, testID)){
			TestAtt attendance = new TestAtt();
			attendance.setAtt_id(testAccess.getLastAttendanceID()+1);
			attendance.setDate(new Date());
			attendance.setStudent_id(studentID);
			attendance.setTest_id(testID);
			
			testAccess.insertAttendance(attendance);
		}else{ throw new Exception("Student has already attended this test");
		
	}
	}

	public TestDisplayDetails getTestData(int testID, int studentID) throws SQLException, NamingException {
		
		TestDisplayDetails displayDetails = new TestDisplayDetails();		
		TestAccess access = new TestAccess();
		
		List<StudentAnswers> sa = new ArrayList<StudentAnswers>();
		
		Tests tests = new TestSetsAccess().getTestDetailsUsingID(testID);
		TestNameAndId testNameAndId = new TestNameAndId();
		testNameAndId.setId(tests.getTest_id());
		testNameAndId.setTestName(tests.getTest_name());
		
		displayDetails.setAnswers(sa);
		displayDetails.setQuestionList(access.getAllQuestionsForATest(testID));
		displayDetails.setTestNameAndId(testNameAndId);
		displayDetails.setStartTime(new Date());
		displayDetails.setMinutesLeft(tests.getTest_duration());
		displayDetails.setStudentID(studentID);
		
		return displayDetails;
	}

	public int addAndResult(List<StudentAnswers> answers) throws SQLException, NamingException {
		
		int result=0;
		TestAccess testAccess = new TestAccess();
		for(StudentAnswers ans : answers){
		  if(testAccess.isCorrectAnswer(ans)){
			  result++;
		  }
		}
		
		return result;
		
		
	}
}
